package com.example.musicplayer;

import java.util.ArrayList;
import java.util.List;

import com.example.musicplayer.bean.Music;
import com.example.musicplayer.bean.MusicAdapter;
import com.example.musicplayer.utils.UIHelper;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity implements OnItemClickListener {

    private static final String LOG_TAG = MainActivity.class.getSimpleName();
    private ListView mListView;
    private MusicListAdapter mListAdapter;
    private ArrayList<Music> mMusics;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initViews();
        initData();

        mListView.setAdapter(mListAdapter);

        initEvent();
    }

    private void initViews() {
        mListView = (ListView) findViewById(android.R.id.list);
    }

    private void initData() {
        MusicAdapter adapter = new MusicAdapter();
        List<Music> musics = adapter.getMusics(getApplicationContext());

        if (musics != null) {
            mMusics = new ArrayList<Music>(musics);
        } else {
            mMusics = new ArrayList<Music>();
        }

        mListAdapter = new MusicListAdapter(MainActivity.this);
    }

    private void initEvent() {
        mListView.setOnItemClickListener(this);
    }

    private class MusicListAdapter extends BaseAdapter {

        private LayoutInflater mLayoutInflater;

        public MusicListAdapter(Context context) {
            mLayoutInflater = LayoutInflater.from(context);
        }

        @Override
        public int getCount() {
            return mMusics != null ? mMusics.size() : 0;
        }

        @Override
        public Object getItem(int position) {
            return mMusics != null ? mMusics.get(position) : null;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view = null;
            ViewHolder viewHolder = null;
            if (convertView == null) {
                view = mLayoutInflater.inflate(R.layout.layout_music_list_item, parent, false);
                viewHolder = new ViewHolder();
                viewHolder.titleView = (TextView) view.findViewById(R.id.music_name);
                view.setTag(viewHolder);
            } else {
                view = convertView;
                viewHolder = (ViewHolder) view.getTag();
            }

            bindView(position, viewHolder);

            return view;
        }

        private void bindView(int position, ViewHolder viewHolder) {
            Music music = (Music) getItem(position);
            if (music != null) {
                viewHolder.titleView.setText(music.getTitle());
            }
        }

        private class ViewHolder {
            TextView titleView;
            TextView dateView;
            TextView durationView;
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Music music = mMusics.get(position);
        if (music != null) {
//            Uri uri = Uri.withAppendedPath(MusicConstant.MEDIA_EXTERNAL_CONTENT_URI,
//                    String.valueOf(music.getId()));
//
//            UIHelper.viewAndPlayMusic(MainActivity.this, uri, music.getDisplayName());
            UIHelper.viewAndPlayMusic(MainActivity.this, mMusics, position);
        } else {
            Toast.makeText(MainActivity.this, "Music not exists", Toast.LENGTH_SHORT).show();
        }
    }
}
